package com.jsbb.service.sys;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jsbb.dto.sys.SysRoleQueryDto;
import com.jsbb.dto.sys.SysRoleSaveDto;
import com.jsbb.dto.sys.SysRoleUpdateDto;
import com.jsbb.exception.ServiceException;
import com.jsbb.mapper.sys.SysRoleMapper;
import com.jsbb.mapper.sys.SysRoleMenuMapper;
import com.jsbb.model.sys.SysRole;
import com.jsbb.model.sys.SysRoleMenu;
import com.jsbb.util.IdWorkerUtils;
import com.jsbb.vo.PageVo;
import com.jsbb.vo.sys.SysRoleDetailVo;
import com.jsbb.vo.sys.SysRoleListVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 角色Service
 *
 * @author w
 * @date 2021-03-05
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class SysRoleService {

    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;

    public List<SysRoleListVo> selectAll() {
        List<SysRole> list = sysRoleMapper.selectByCondition(null);
        List<SysRoleListVo> roleVoList = new ArrayList<>();
        for (SysRole sysRole : list) {
            SysRoleListVo roleVo = new SysRoleListVo();
            BeanUtils.copyProperties(sysRole, roleVo);
            roleVoList.add(roleVo);
        }
        return roleVoList;
    }

    public PageVo<SysRoleListVo> list(SysRoleQueryDto queryDto) {
        Page page = PageHelper.startPage(queryDto.getPageNo(), queryDto.getPageSize());
        List<SysRole> list = sysRoleMapper.selectByCondition(queryDto);
        List<SysRoleListVo> roleVoList = new ArrayList<>();
        for (SysRole sysRole : list) {
            SysRoleListVo roleVo = new SysRoleListVo();
            BeanUtils.copyProperties(sysRole, roleVo);
            roleVoList.add(roleVo);
        }
        return new PageVo((int) page.getTotal(), roleVoList);
    }

    public SysRoleDetailVo get(Long id) {
        SysRole sysRole = sysRoleMapper.selectByPrimaryKey(id);
        if (sysRole == null) {
            throw new ServiceException("数据不存在");
        }
        SysRoleDetailVo sysRoleDetailVo = new SysRoleDetailVo();
        BeanUtils.copyProperties(sysRole, sysRoleDetailVo);
        sysRoleDetailVo.setMenuId(sysRoleMenuMapper.selectByRoleId(id));
        return sysRoleDetailVo;
    }

    public void save(SysRoleSaveDto saveDto) {
        //新增角色
        SysRole sysRole = new SysRole();
        sysRole.setId(IdWorkerUtils.idWorker.nextId());
        sysRole.setRoleName(saveDto.getRoleName());
        sysRole.setRemark(saveDto.getRemark());
        sysRole.setCreateTime(new Date());
        sysRole.setUpdateTime(new Date());
        sysRoleMapper.insert(sysRole);

        //新增角色权限
        List<Long> menuList = saveDto.getMenuId();
        if (menuList != null && menuList.size() > 0) {
            SysRoleMenu sysRoleMenu = new SysRoleMenu();
            sysRoleMenu.setRoleId(sysRole.getId());
            for (Long menuId : menuList) {
                sysRoleMenu.setId(IdWorkerUtils.idWorker.nextId());
                sysRoleMenu.setMenuId(menuId);
                sysRoleMenuMapper.insert(sysRoleMenu);
            }
        }
    }

    public void delete(Long id) {
        //删除角色
        sysRoleMapper.deleteByPrimaryKey(id);
        //删除角色菜单
        sysRoleMenuMapper.deleteByRoleId(id);
    }

    public void update(SysRoleUpdateDto updateDto) {
        SysRole sysRole = sysRoleMapper.selectByPrimaryKey(updateDto.getId());
        if (sysRole == null) {
            throw new ServiceException("数据不存在");
        }
        //修改角色
        sysRole.setRoleName(updateDto.getRoleName());
        sysRole.setRemark(updateDto.getRemark());
        sysRoleMapper.updateByPrimaryKey(sysRole);

        //删除原有角色菜单
        sysRoleMenuMapper.deleteByRoleId(updateDto.getId());

        //新增新的角色菜单
        if (updateDto.getMenuId() != null && updateDto.getMenuId().size() > 0) {
            SysRoleMenu sysRoleMenu = new SysRoleMenu();
            sysRoleMenu.setRoleId(updateDto.getId());
            List<Long> menuIdList = updateDto.getMenuId();
            for (Long menuId : menuIdList) {
                sysRoleMenu.setId(IdWorkerUtils.idWorker.nextId());
                sysRoleMenu.setMenuId(menuId);
                sysRoleMenuMapper.insert(sysRoleMenu);
            }
        }
    }
}
